﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using Model;
using DataAccess;
using System.Text;
using Util.PageInfo;
using ValidateCommon;
using Util.BasePage;

namespace RichECMS.ServerPages.Js
{
    public partial class List : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                State.intPageIndex = 1;
                InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
            }
        }

        private void InitDataBind(int intCurrentPage, int intPageSize)
        {
            try
            {
                using (ScriptsFactory objFactory = new ScriptsFactory())
                {
                    StringBuilder strWhere = new StringBuilder();
                    int intPageFrom = intCurrentPage * intPageSize;
                    int intTotalCounts = 0;
                    BusinessLogic.BusinessRules.Scripts objScript = objFactory.NewInstance();
                    if (State.strWhereSql != "")
                    {
                        intTotalCounts = objScript.GetTotalCount(State.strWhereSql);
                    }
                    else
                    {
                        intTotalCounts = objScript.GetTotalCount();
                    }

                    LitPageCurrent.Text = intCurrentPage.ToString();
                    LitPageCount.Text = intTotalCounts.ToString();
                    if (intTotalCounts % intPageSize != 0)
                    {
                        if ((intCurrentPage == intTotalCounts / intPageSize + 1))
                        {
                            intPageFrom = (intCurrentPage - 1) * intPageSize;
                            intPageSize = intTotalCounts % intPageSize;
                            intPageFrom = intPageFrom + intPageSize;
                        }
                    }
                    if (State.strWhereSql.Trim() == "")
                    {
                        strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Scripts   order by intScriptsID desc )t order by intScriptsID asc)m order by intScriptsID asc)mm  order by intScriptsID desc");
                    }
                    else
                    {
                        strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " * from Scripts  where" + State.strWhereSql + " order by intScriptsID desc )t order by intScriptsID asc)m order by intScriptsID asc)mm  order by intScriptsID desc");
                    }
                    objScript = objFactory.CreatorByFullSql(strWhere.ToString());
                    if (objScript != null)
                    {
                        rptList.DataSource = objScript.Data.Scripts.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = intTotalCounts;
                        rptList.DataBind();
                    }
                    else
                    {
                        rptList.DataSource = objFactory.NewInstance().Data.Scripts.DataSet.Tables[0].DefaultView;
                        AspNetPagerList.RecordCount = 0;
                        rptList.DataBind();
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }

        }


        //搜索
        protected void ImageButtonsearch(object sender, ImageClickEventArgs e)
        {
            StringBuilder sbWhere = new StringBuilder();
            if (txtTitle.Value.Trim() != "")
            {
                sbWhere.AppendFormat(" chvScriptsName like '%{0}%'", txtTitle.Value.Trim());
            }
            State.strWhereSql = sbWhere.ToString();
            State.intPageIndex = 1;
            InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSort_Click(object sender, ImageClickEventArgs e)
        {
            if (SortHidden.Value != "")
            {
                //Response.Redirect("ArticleSort.aspx?SortID=" + SortHidden.Value);
            }
            else
            {
                Alert("请选中要排序的文章！");
            }
        }
        /// <summary>
        /// 删除所选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    int i = objFactory.DeleteArticleWhere(" intArticleID in (" + SortHidden.Value.Trim() + ")");
                    if (i != 0)
                    {
                        InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                    }
                    else
                    {
                        Alert("删除失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }
        /// <summary>
        /// 批量放入回收站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecovery_Click(object sender, ImageClickEventArgs e)
        {

        }

        //分页
        protected void AspNetPagerArticle_PageChanged(object sender, EventArgs e)
        {
            State.intPageIndex = AspNetPagerList.CurrentPageIndex;
            InitDataBind(AspNetPagerList.CurrentPageIndex, AspNetPagerList.PageSize);
        }

        /// <summary>
        /// rptList_ItemCommand  编辑   放入回收站  删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                try
                {
                    using (ScriptsFactory objFactory = new ScriptsFactory())
                    {
                        //int i = objFactory.de(int.Parse(e.CommandArgument.ToString()));
                        //if (i != 0)
                        //{
                        //    InitDataBind(State.intPageIndex, AspNetPagerList.PageSize);
                        //}
                        //else
                        //{
                        //    Alert("删除失败！");
                        //}
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
            //放入回收站
            if (e.CommandName == "Recycle")
            {

            }
        }
    }
}